package de.uni.muenster.idw2;

import java.util.Collections;
import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import de.uni.muenster.idw2.data.Building;
import de.uni.muenster.idw2.data.Floor;
import de.uni.muenster.idw2.data.Room;

public class RouteSelector extends Activity{
	
	private Building building;
	
	private Spinner spinnerStart;
	private Spinner spinnerDestination;
	private AutoCompleteTextView autoCompleteTextViewStart;
	private AutoCompleteTextView autoCompleteTextViewDestination;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.route_selector);
		
		/**
		 * load the builing
		 */
		
		building = Building.getBuilding();
		
		
		spinnerStart = (Spinner) findViewById(R.id.spinnerStart);
		spinnerDestination = (Spinner) findViewById(R.id.spinnerDestination);
		autoCompleteTextViewDestination = (AutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextViewDestination);
		autoCompleteTextViewStart = (AutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextViewStart);
		
		/**
		 * load the start and dest points into the spinner
		 */
		if(building != null){
			LinkedList<String> adapterItemsStart = new LinkedList<String>();
			LinkedList<String> adapterItemsDestination = new LinkedList<String>();
			
			LinkedList<Floor> floors = building.getFloors();
			for (Floor floor : floors) {
				LinkedList<Room> rooms = floor.getRooms();
				for (Room room : rooms) {
					String name = room.getName();
					adapterItemsStart.add(name);
					adapterItemsDestination.add(name);
				}
			}
			
			Collections.sort(adapterItemsDestination);
			Collections.sort(adapterItemsStart);
			
			adapterItemsStart.addFirst("Current Location");
			
			ArrayAdapter adapterStart = new ArrayAdapter(this, android.R.layout.simple_spinner_item, adapterItemsStart);
			ArrayAdapter adapterDestination = new ArrayAdapter(this, android.R.layout.simple_spinner_item, adapterItemsDestination);
			
			adapterStart.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			adapterDestination.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			
			spinnerStart.setAdapter(adapterStart);
			spinnerDestination.setAdapter(adapterDestination);
			
			
			ArrayAdapter adapterStartAuto = new ArrayAdapter(this, R.layout.list_item, adapterItemsStart);
			ArrayAdapter adapterDestinationAuto = new ArrayAdapter(this, R.layout.list_item, adapterItemsDestination);
			
			autoCompleteTextViewStart.setAdapter(adapterStartAuto);
			autoCompleteTextViewDestination.setAdapter(adapterDestinationAuto);
			
		}
		
	}
	
    public void startNavigation(View view){
    	Intent intent = new Intent(this, Navigation.class);
    	intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    	startActivity(intent);
    	finish();
    }
}
